package io;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;
import java.util.StringTokenizer;
import util.Timer;
import java.util.Vector;

import ship.Ship;
import ship.ShipConstants;

import main.Game;

public class ShipInputListener {
	private SeanKeyListener listen = SeanKeyListener.getInstance();

	public static final int UP = 38;
	public static final int DOWN = 39;
	public static final int LEFT = 37;
	public static final int RIGHT = 40;

	public static final int BACKSLASH = 92;

	public static final int E = 69;
	public static final int T = 84;
	public static final int H = 72;
	public static final int S = 83;
	public static final int W = 87;
	public static final int PLUS = 61;
	public static final int MINUS = 45;

	private int currentSystem = 0;

	private Timer timer = new Timer(5, false);

	public ShipInputListener() {
		Game.game.getWindow().addKeyListener(listen);
		timer.start();
	}

	public void update() {

		if(listen.isKeyDown(E)) {
			currentSystem = ShipConstants.ENGINES;
		}
		if(listen.isKeyDown(T)) {
			currentSystem = ShipConstants.THRUSTERS;
		}
		if(listen.isKeyDown(H)) {
			currentSystem = ShipConstants.SHIELDS;
		}
		if(listen.isKeyDown(S)) {
			currentSystem = ShipConstants.SENSORS;
		}
		if(listen.isKeyDown(W)) {
			currentSystem = ShipConstants.WEAPONS;
		}
		if(listen.isKeyDown(39)) {
			Game.game.getShip().rotate(1);
		}
		if(listen.isKeyDown(38)) {
			Game.game.getShip().acc(1);
		}
		if(listen.isKeyDown(37)) {
			Game.game.getShip().rotate(-1);
		}
		if(listen.isKeyDown(40)) {
			Game.game.getShip().acc(-1);
		}
		if(listen.isKeyDown(BACKSLASH)) {
			Game.game.getShip().jump();
		}
		if(listen.isKeyDown(32)) {
			Game.game.getShip().fire(true, true);
		}
		timer.count();
		if(timer.ready()) {
			if(listen.isKeyDown(MINUS)) {
				Game.game.getShip().decreasePower(currentSystem);
				timer.reset();
			}
			if(listen.isKeyDown(PLUS)) {
				Game.game.getShip().increasePower(currentSystem);
				timer.reset();
			}

		}


		listen.update();
	}

	public void recieveMessage(String s) {
		StringTokenizer tok = new StringTokenizer(s);
		String type = tok.nextToken();
		int player = (int) Double.parseDouble(tok.nextToken());
		Ship ship = Game.game.getOtherShip(player);
		if(type.equals("Rotate")) {
			ship.rotate( (int) Double.parseDouble(tok.nextToken()));
		}
		if(type.equals("Acc")) {
			ship.acc( (int) Double.parseDouble(tok.nextToken()));
		}
		if(type.equals("Jump")) {
			ship.jump();
		}
		if(type.equals("Fire")) {
			ship.fire(true, true);
		}
	}
}
